/**
 * OperationReportAction.java
 * Created at 2017-11-28
 * Created by zhanghailin
 * Copyright (C) 2017 BROADTEXT SOFTWARE, All rights reserved.
 */
package com.broadtext.rms.system.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.broadtext.common.exp.DaoException;
import com.broadtext.common.exp.ServiceException;
import com.broadtext.common.utils.StringUtils;
import com.broadtext.rms.basic.service.ITmCustomService;
import com.broadtext.rms.system.service.IBatteryLogService;
import com.broadtext.rms.system.service.IOperationReportService;
import com.broadtext.rms.system.service.ISysUserService;

/**
 * <p>ClassName: OperationReportAction</p>
 * <p>Description: TODO</p>
 * <p>Author: zhanghailin</p>
 * <p>Date: 2017-11-28</p>
 */
@Controller
@RequestMapping("/main/report")
public class OperationReportAction {
    
    @Autowired
    @Qualifier("operationReportService")
    private IOperationReportService operationReportService;
    
    /**
     * 用户Service
     */
    @Autowired
    @Qualifier("sysUserService")
    private ISysUserService sysUserService;
    
    @Autowired
    @Qualifier("batteryLogService")
    private IBatteryLogService batteryLogService;
    
    /**
     * o 注入客户service
     */
    @Autowired
    @Qualifier("tmCustomService")
    private ITmCustomService tmCustomService;
    
    /**
     * 
     * <p>Description: TODO</p>
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/getAlertData.mvc")
    @ResponseBody
    public List<Object> getAlertData(HttpServletRequest request, HttpServletResponse response) throws ServiceException{
        Map<String, Object> paramsMap = new HashMap<String, Object>();
        String customerId = request.getParameter("customerId");
        String gatherTimeStart = request.getParameter("gatherTimeStart");
        String gatherTimeEnd = request.getParameter("gatherTimeEnd");
        if(StringUtils.isNotBlank(customerId)){
            paramsMap.put("customerId", customerId);
        }
        if(StringUtils.isNotBlank(gatherTimeStart)){
            paramsMap.put("gatherTimeStart", gatherTimeStart);
        }
        if(StringUtils.isNotBlank(gatherTimeEnd)){
            paramsMap.put("gatherTimeEnd", gatherTimeEnd);
        }
        List<Object> list = new ArrayList<Object>();
        list = this.operationReportService.getAlertData(paramsMap);
        return list;
    }
    
    
    /**
     * 
     * <p>Description: TODO</p>
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/getAlertPieChartData.mvc")
    @ResponseBody
    public Map<String, Object> getAlertPieChartData(HttpServletRequest request, HttpServletResponse response) throws ServiceException{
        Map<String, Object> map = new HashMap<String, Object>();
        Map<String, Object> paramsMap = new HashMap<String, Object>();
        String customerId = request.getParameter("customerId");
        String gatherTimeStart = request.getParameter("gatherTimeStart");
        String gatherTimeEnd = request.getParameter("gatherTimeEnd");
        if(StringUtils.isNotBlank(customerId)){
            paramsMap.put("customerId", customerId);
        }
        if(StringUtils.isNotBlank(gatherTimeStart)){
            paramsMap.put("gatherTimeStart", gatherTimeStart);
        }
        if(StringUtils.isNotBlank(gatherTimeEnd)){
            paramsMap.put("gatherTimeEnd", gatherTimeEnd);
        }
        map = this.operationReportService.getAlertPieChartData(paramsMap);
        return map;
    }
    
    /**
     * 
     * <p>Description: TODO</p>
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/getSaleSupportChartData.mvc")
    @ResponseBody
    public Map<String, Object> getSaleSupportChartData(HttpServletRequest request, HttpServletResponse response) throws ServiceException{
        Map<String, Object> map = new HashMap<String, Object>();
        Map<String, Object> paramsMap = new HashMap<String, Object>();
        String customerId = request.getParameter("customerId");
        String gatherTimeStart = request.getParameter("gatherTimeStart");
        String gatherTimeEnd = request.getParameter("gatherTimeEnd");
        if(StringUtils.isNotBlank(customerId)){
            paramsMap.put("customerId", customerId);
        }
        if(StringUtils.isNotBlank(gatherTimeStart)){
            paramsMap.put("gatherTimeStart", gatherTimeStart);
        }
        if(StringUtils.isNotBlank(gatherTimeEnd)){
            paramsMap.put("gatherTimeEnd", gatherTimeEnd);
        }
        map = this.operationReportService.getSaleSupportChartData(paramsMap);
        return map;
    }
    
    /**
     * 
     * <p>Description: TODO</p>
     * @param request
     * @return
     * @throws ServiceException
     */
    @RequestMapping(value = "/getCustomerOpt.mvc")
    @ResponseBody
    public Map<String, Object>  getCustomerOpt(HttpServletRequest request) throws ServiceException{
        Map<String, Object> map;
        map = new HashMap<String, Object>();
        List<Object> list;
        String customerId = ""; 
        //判断当前登录人是否是客户账号，进行数据过滤
        //若新增客户，且当前操作账号为客户类型，新增客户时选择上级客户只能选本级或本下级
        Map<String, Object> checkMap;
        try {
            checkMap = this.sysUserService.checkUserClass(request.getSession().getAttribute("sys.user").toString());
            if(null != checkMap && "true".equals(checkMap.get("flag").toString())){
                customerId = checkMap.get("cusId")+"";
            }
        } catch (DaoException e1) {
            e1.printStackTrace();
        }
        list = this.operationReportService.getCustomerOpt(customerId);
        map.put("dict", list);
        return map;
    }
    
    /**
     * 
     * <p>Description: TODO</p>
     * @param request
     * @return
     * @throws ServiceException
     */
    @RequestMapping(value = "/getCustomerId.mvc")
    @ResponseBody
    public Map<String, Object>  getCustomerId(HttpServletRequest request) throws ServiceException{
        Map<String, Object> map;
        map = new HashMap<String, Object>();
        String customerId = ""; 
        //判断当前登录人是否是客户账号，进行数据过滤
        //若新增客户，且当前操作账号为客户类型，新增客户时选择上级客户只能选本级或本下级
        Map<String, Object> checkMap;
        try {
            checkMap = this.sysUserService.checkUserClass(request.getSession().getAttribute("sys.user").toString());
            if(null != checkMap && "true".equals(checkMap.get("flag").toString())){
                customerId = checkMap.get("cusId")+"";
            }
        } catch (DaoException e1) {
            e1.printStackTrace();
        }
        map.put("customerId", customerId);
        return map;
    }
    
    /**
     * 
     * <p>Description: 查询电池基本信息</p>
     * @param request
     * @return
     * @throws ServiceException
     */
    @RequestMapping(value = "/getBatteryBasicInfo.mvc")
    @ResponseBody
    public Map<String, Object>  getBatteryBasicInfo(HttpServletRequest request) throws ServiceException{
        Map<String, Object> map;
        map = new HashMap<String, Object>();
        Map<String, Object> queryMap = new HashMap<String, Object>();
        String customerId = request.getParameter("customerId");
        String gatherTimeStart = request.getParameter("gatherTimeStart");
        String gatherTimeEnd = request.getParameter("gatherTimeEnd");
        if(StringUtils.isNotBlank(customerId)){
            queryMap.put("customerId", customerId);
        }
        if(StringUtils.isNotBlank(gatherTimeStart)){
            queryMap.put("gatherTimeStart", gatherTimeStart);
        }
        if(StringUtils.isNotBlank(gatherTimeEnd)){
            queryMap.put("gatherTimeEnd", gatherTimeEnd);
        }
        map = this.operationReportService.getBatteryBasicInfo(queryMap);
        //查询电池充电次数
        //判断登录账户类型，如果为客户账户则过滤数据，客户类型只看本级和本下级
        Map<String, Object> checkMap;
        List<String> idList;
        try {
            checkMap = this.sysUserService.checkUserClass(request.getSession().getAttribute("sys.user").toString());
            if(null != checkMap && "true".equals(checkMap.get("flag").toString())){
                //该用户是客户账号
                queryMap.put("cusId", checkMap.get("cusId"));
                //查询当前登录用户所有下属子节点pk_id
                idList = this.tmCustomService.getChildCusIdArray(checkMap.get("cusId")+"");
                queryMap.put("ids", idList);
            }
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        String chargeNum = String.valueOf(this.batteryLogService.getRecordsTotal(queryMap));
        map.put("chargeNum", chargeNum);
        return map;
    }

}
